/**
 * 功能描述 表单
 */

define(function (require, exports, module){
	var EditGrid=require("editgrid");
	var Dialog = require("dialog");
	var store =require("store");
	var activeUser = store.get("user");
	var moment = require("moment");
	
	var instance = {
			initComponent: function (params,widget){
				var da = params;
				var optiongrid = new EditGrid({
					parentNode:".J-optiongrid",
					autoRender:false,
					model:{
						id:"optiongrid",
						onAdd: function(editors) {
							var destination = editors["destination.name"];
				            var pkDestination = destination.getValue();
				            var startDate = editors["startDate"].getValue();
				            var endDate = editors["endDate"].getValue();
				            var limitmax = editors["limitmax"].getValue();
				            var maxpeople = editors["maxpeople"].getValue();
				            var limitmin = editors["limitmin"].getValue();
				            var minpeople = editors["minpeople"].getValue();
				            
				            if(limitmax == "false"){
			            		optiongrid.getPlugins()["maxpeople"].setDisabled(true);
			            	}else{
			            		optiongrid.getPlugins()["maxpeople"].setDisabled(false);
			            	}
			            	if(limitmin == "false"){
			            		optiongrid.getPlugins()["minpeople"].setDisabled(true);
			            	}else{
			            		optiongrid.getPlugins()["minpeople"].setDisabled(false);
			            	}
				            
			            	
			            	if(limitmax == "true" && limitmin == "true"){
			            		if(Number(maxpeople) < Number(minpeople)){
			            			Dialog.alert({content:"成团最多人数不能小于成团最少人数"});
			            			return false;
			            		}
			            	}
			            	
//			            	if(moment(startDate).isBefore(moment().add(1,"days"))){
//			            		Dialog.alert({
//			            			content:"开始时间应大于今天"
//			            				});
//			            		return false;
//			            	}
//			            	
			            	
			            	if(startDate && endDate && startDate> endDate){
			            		if($(".el-dialog-modal").length == 0){
			            			Dialog.alert({content:"开始时间不能大于结束时间"});	
			            		}
			            		return false;
			            	}
			            	
				            if(pkDestination && startDate && endDate && limitmax && limitmin){
				            	optiongrid.add({
					            	destination: destination.getData(pkDestination),
					            	startDate: startDate,
					            	endDate: endDate,
					            	limitmax: editors["limitmax"].getData(limitmax),
					            	maxpeople: maxpeople,
					            	limitmin: editors["limitmin"].getData(limitmin),
					            	minpeople: minpeople
					            });
				            	return true;
				            }else{
				            	return false;
				            }
				            
				        },
						columns:[{
							name:"destination.name",
							label:"目的地",
							editor:{
								type:"select",
								url:"api/destination/query",
								params:function(){
									var obj={
			    	     				fetchProperties:"pkDestination,name"
			                		}
			                		return obj;
								},
								keyField:"pkDestination",
								valueField:"name",
								onEdit : function(editor,rowIndex,rowData){
									editor.setValue(rowData.destination);
								},
								onChange : function(plugin,index,rowData){
									rowData.destination = plugin.getData(plugin.getValue());
									optiongrid.update(index,rowData);
								}
							}
						},{
							name:"startDate",
							label:"开始日期",
							format:"date",
							
							formatparams:{
								mode:"YYYY-MM-DD"
							},
							editor:{
								type:"date",
								mode:"YYYY-MM-DD",
								onSelectDate:function(date,element){
									var idate = moment(date).format("YYYY-MM-DD");
									var newDate = moment(new Date()).format("YYYY-MM-DD");
									if(idate <= newDate){
										Dialog.alert({content:"开始时间应大于今天"});
										$(element).val("");
									}
								},
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.startDate);
								},
								onChange:function(plugin,index,rowData){
									if((rowData.endDate - moment()) < 0 && rowData.pkPlanItem != null){
										Dialog.confirm({
											content:"当前团状态为已结束,您确定要修改么?",
											confirm : function(){
												rowData.startDate = plugin.getValue();
												optiongrid.update(index,rowData);
												},
												cancel:function(){
													Dialog.close();
												}
										});
									}else{
										rowData.startDate = plugin.getValue();
										optiongrid.update(index,rowData);
									}
								}
							}
						},{
							name:"endDate",
							label:"结束日期",
							format:"date",
							formatparams:{
								mode:"YYYY-MM-DD"
							},
							editor:{
								type:"date",
								mode:"YYYY-MM-DD",
								minDate: "",
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.endDate);
								},
								onChange:function(plugin,index,rowData){
									if((rowData.endDate - moment()) < 0 && rowData.pkPlanItem != null){
										Dialog.confirm({
											content:"当前团状态为已结束,您确定要修改么?",
											confirm : function(){
												rowData.endDate = plugin.getValue();
												optiongrid.update(index,rowData);
												},
												cancel:function(){
													Dialog.close();
												}
										});
									}else{
										rowData.endDate = plugin.getValue();
										optiongrid.update(index,rowData);
									}
								}
							}
						},{
							name:"limitmax",
							label:"是否限制最多人数",
							editor:{
								type:"select",
								options:[{
									key:"true",
									value:"限制"
								},{
									key:"false",
									value:"不限制"
								}],
								defaultValue:"false",
								onEdit:function(editor,rowIndex,rowData){
									if(rowData.limitmax.key){
										editor.setValue(rowData.limitmax.key);
									}else{
										editor.setValue(rowData.limitmax);
									}
								},
								onChange:function(plugin,index,rowData){
									rowData.limitmax = plugin.getData(plugin.getValue());
									optiongrid.update(index,rowData);
								}
							},
							format:function(value,row){
								if(typeof value == "object"){
									return value.value;
								}else{
									if(value == true || value == "true"){
										return "限制";
									}else{
										return "不限制";
									}
								}
							}
						},{
							name:"maxpeople",
							label:"成团最多人数",
							editor:{
								type:"text",
								onBeforeEdit:function(rowData){
									if(rowData.limitmax == true || rowData.limitmax.key=="true"){
										return true;
									}else{
										return false;
									}
								},
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.maxpeople);
								},
								onChange:function(plugin,index,rowData){
									
									var limitmax = rowData.limitmax.key;
						            var maxpeople = plugin.getValue();
						            var limitmin = rowData.limitmin.key;
						            var minpeople = rowData.minpeople;
						            
									if(limitmax == "true" && limitmin == "true"){
					            		if(Number(maxpeople) < Number(minpeople)){
					            			Dialog.alert({content:"成团最多人数不能小于成团最少人数"});
					            			return false;
					            		}
					            	}
									
									rowData.maxpeople = plugin.getValue();
									optiongrid.update(index,rowData);
								}
							}
						},{
							name:"limitmin",
							label:"是否限制最少人数",
							editor:{
								type:"select",
								options:[{
									key:"true",
									value:"限制"
								},{
									key:"false",
									value:"不限制"
								}],
								defaultValue:"false",
								onEdit:function(editor,rowIndex,rowData){
									if(rowData.limitmax.key){
										editor.setValue(rowData.limitmin.key);
									}else{
										editor.setValue(rowData.limitmin);
									}
								},
								onChange:function(plugin,index,rowData){
									rowData.limitmin = plugin.getData(plugin.getValue());
									optiongrid.update(index,rowData);
								}
							},
							format:function(value,row){
								if(typeof value == "object"){
									return value.value;
								}else{
									if(value == true || value == "true"){
										return "限制";
									}else{
										return "不限制";
									}
								}
							}
						},{
							name:"minpeople",
							label:"成团最少人数",
							editor:{
								type:"text",
								onBeforeEdit:function(rowData){
									if(rowData.limitmin == true || rowData.limitmin.key=="true"){
										return true;
									}else{
										return false;
									}
								},
								onEdit:function(editor,rowIndex,rowData){
									editor.setValue(rowData.minpeople);
								},
								onChange:function(plugin,index,rowData){
									var limitmax = rowData.limitmax.key;
						            var maxpeople = rowData.maxpeople;
						            var limitmin = rowData.limitmin.key;
						            var minpeople = plugin.getValue();
						            
									if(limitmax == "true" && limitmin == "true"){
					            		if(Number(maxpeople) < Number(minpeople)){
					            			Dialog.alert({content:"成团最多人数不能小于成团最少人数"});
					            			return false;
					            		}
					            	}
									
									
									rowData.minpeople = plugin.getValue();
									optiongrid.update(index,rowData);
								}
							}
						},{
							name:"operate",
							label:"操作",
							format:"button",
							formatparams:[{
								id:"copyrow",
								text:"复制",
								/*show:false,function(value,row){
									if(row.status.key == "Disable"){
										return true;
									}else{
										return false;
									}
								},*/
								show:function(){
									if(da==null || da==""){
										return true;
									}else{
										return false;
									}
								},
								handler:function(index,data,rowEle){
	  								var datas=widget.get("optiongrid").getData();
	  								widget.get("optiongrid").add({
	  									destination:datas[index].destination,
	  									startDate:datas[index].startDate,
	  									endDate:datas[index].endDate,
	  									limitmax:datas[index].limitmax,
	  									maxpeople:datas[index].maxpeople,
	  									limitmin:datas[index].limitmin,
	  									minpeople:datas[index].minpeople,
	  								});
	  							}
							},{
								id:"del",
								icon:"icon-remove",
								/*show:false,function(value,row){
									if(row.status.key == "Disable"){
										return true;
									}else{
										return false;
									}
								},*/
								show:function(){
									if(da==null || da==""){
										return true;
									}else{
										return false;
									}
								},
								handler:function(index,data,rowEle){
									if((data.endDate - moment()) < 0 && data.pkPlanItem != null){
										Dialog.confirm({
											content:"当前团状态为已结束,您确定要删除么?",
											confirm : function(){
												optiongrid.remove(rowEle);
		  									},
		  									cancel:function(){
		  										Dialog.close;
		  									}
										});
									}else{
										optiongrid.remove(rowEle);
									}
	  							}
							}]
						}]
					}
				});
				widget.set("optiongrid",optiongrid);
			}
	};
	module.exports = instance;
});
